package com.example.zyfypt516wh.model;

import com.example.zyfypt516wh.service.DetailService;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.DetailIface;
import com.example.zyfypt516wh.iface.DetailListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DetailModel implements DetailIface {
    private Retrofit retrofit;
    public DetailModel() {
        //使用 Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL).addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getDetail(String mod,String id, String sessionID, DetailListener listener) {

        //使用 Retrofit----2
        DetailService service=retrofit.create(DetailService.class);
        Call<ResBean> call
                =service.getDetail(mod,id,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<ResBean>() {
            @Override
            public void onResponse(Call<ResBean> call, Response<ResBean> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<ResBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
}
